<template>
    <a-modal :title="$t('编辑快照复制平台')" v-model='isShow' :maskClosable='false' v-dragModal @cancel="handleClose">
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-spin :spinning="loading">
                <a-form-model-item :label="$t('名称')" prop="name">
                    <a-input v-model="form.name" :placeholder="$t('Enter') + $t('名称')" />
                </a-form-model-item>
                <a-form-model-item :label="$t('目标集群')" prop="remote_cluster_name">
                    <a-select disabled v-model="form.remote_cluster_name" :placeholder="$t('Choose') + $t('目标集群')">
                        <a-select-option v-for="item of clusterOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item :label="$t('保护网关')" prop="remote_protect_gateway">
                    <a-select disabled v-model="form.remote_protect_gateway" :placeholder="$t('Choose') + $t('保护网关')">
                        <a-select-option v-for="item of gatewayOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item :label="$t('备份存储池')" prop="remote_cluster_pool">
                    <a-select disabled v-model="form.remote_cluster_pool" :placeholder="$t('Choose') + $t('备份存储池')">
                        <a-select-option v-for="item of bucketOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
            </a-spin>
        </a-form-model>
        <template slot="footer">
            <!-- <a-button :disabled="testDisabled" type='primary' @click="handleTest">{{ $t('有效性测试') }}</a-button> -->
            <a-button type='primary' @click="handleCreate">{{ $t('保存') }}</a-button>
            <a-button @click="handleClose">{{ $t('Cancel') }}</a-button>
        </template>
    </a-modal>
</template>

<script>
export default {
    data() {
        return {
            isShow: false,
            loading: false,
            labelCol: { span: 6 },
            wrapperCol: { span: 18 },
            form: {
                name: '',
                remote_cluster_name: undefined,
                remote_protect_gateway: undefined,
                remote_cluster_pool: undefined,
            },
            rules: {
                name: [
                    { required: true, message: this.$t('Enter') + this.$t('名称'), trigger: 'blur' },
                    { pattern: /^[A-Za-z0-9_]{1,128}$/, message: this.$t('名称仅支持大小写英文字母 + 数字 + 英文下划线，1-128 个字符'), trigger: 'blur' }
                ],
                remote_cluster_name: [
                    { required: true, message: this.$t('Choose') + this.$t('目标集群'), trigger: 'change' },
                ],
                remote_protect_gateway: [
                    { required: true, message: this.$t('Choose') + this.$t('保护网关'), trigger: 'change' },
                ],
                remote_cluster_pool: [
                    { required: true, message: this.$t('Choose') + this.$t('备份存储池'), trigger: 'change' },
                ],
            },
            clusterOptions: [],
            gatewayOptions: [],
            bucketOptions: [],
            testDisabled: false,
            safeDisabled: true,
            nameCheck: false,
        };
    },
    // watch: {
    //     'form.name': {
    //         handler(value) {
    //             this.$nextTick(() => {
    //                 this.$refs.ruleForm.validateField(['name'], valid => {
    //                     if (valid === '') {
    //                         this.nameCheck = true
    //                         this.testDisabled = false
    //                     } else {
    //                         this.testDisabled = true
    //                     }
    //                 })
    //             })
    //         }
    //     },
    // },
    methods: {
        // async handleTest() {
        //     this.loading = true
        //     const { data } = await this.$api.data.getRemoteClusterHealthApi(this.form.cluster)
        //     this.model(data.id) //返回模板
        //     this.notice(data.id, data.params, data.flag) //通知列表显示
        //     if (data.flag == '0') this.safeDisabled = false
        //     this.loading = false
        // },
        handleCreate() {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    this.loading = true
                    const { data } = await this.$api.data.modifyCopyApi(this.form.id,{ name: this.form.name } )
                    this.model(data.id) //返回模板
                    this.notice(data.id, data.params, data.flag) //通知列表显示
                    if (data.flag == '0') {
                        this.$emit('get-list')
                        this.handleClose()
                    }
                    this.loading = false
                } else {
                    return false;
                }
            });
        },
        handleClose() {
            this.$refs['ruleForm'].resetFields();
            this.isShow = false
        },
    }
}
</script>
